﻿class Greeter
{
    element: HTMLElement;
    span: HTMLElement;
    timerToken: number;
    error: boolean = false;
    rstr: string = "";
    constructor(element: HTMLElement)
    {
        this.element = element;
        this.element.innerHTML += "The time is: ";
        this.span = document.createElement('span');
        this.element.appendChild(this.span);
        this.span.innerText = new Date().toUTCString();

        this.element.appendChild(document.createElement('hr'));

        var btn1 = document.createElement('button');
        btn1.textContent = "扣用户 1 anc";
        btn1.onclick = () =>
        {
            if (this.error == false)
            {
                var xhr: XMLHttpRequest = new XMLHttpRequest();
                xhr.onload = (e) =>
                {
                    //this.rstr = xhr.responseText;
                };
                xhr.open("GET", "http://127.0.0.1:22888/use1?", true);
                xhr.send();
            }
        };
        this.element.appendChild(btn1);

        var btn2 = document.createElement('button');
        btn2.textContent = "给用户 5 anc";
        btn2.onclick = () =>
        {
            if (this.error == false)
            {
                var xhr: XMLHttpRequest = new XMLHttpRequest();
                xhr.onload = (e) =>
                {
                    //this.rstr = xhr.responseText;
                };
                xhr.open("GET", "http://127.0.0.1:22888/give5?", true);
                xhr.send();
            }
        };
        this.element.appendChild(btn2);
    }

    start()
    {

        this.timerToken = setInterval(
            () =>
            {
                this.span.innerHTML = new Date().toUTCString() + this.rstr;

                if (this.error == false)
                {
                    var xhr: XMLHttpRequest = new XMLHttpRequest();
                    xhr.onload = (e) =>
                    {
                        this.rstr = xhr.responseText;
                    };
                    xhr.onerror = (e) =>
                    {
                        this.error = true;

                        this.rstr = "error,这个网站只有用小蚁应用商店浏览器打开有意义，请下载";
                    }
                    xhr.open("GET", "http://127.0.0.1:22888/user?", true);
                    xhr.send();
                }
            }
            , 500);

        {
            var xhr: XMLHttpRequest = new XMLHttpRequest();
            xhr.onload = (e) =>
            {
                this.rstr = xhr.responseText;
            };
            xhr.onerror = (e) =>
            {
                this.error = true;

                this.rstr = "error,这个网站只有用小蚁应用商店浏览器打开有意义，请下载";
            }
            xhr.open("GET", "http://127.0.0.1:22888/user?", true);
            xhr.send();
        }
    }

    stop()
    {
        clearTimeout(this.timerToken);
    }

}

window.onload = () =>
{
    var el = document.getElementById('content');
    var greeter = new Greeter(el);
    greeter.start();
};